package com.hualan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hualan.mapper.ArticleMapper;
import com.hualan.pojo.Article;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Result;
import com.hualan.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    public PageResult getArticlesByTitle(String title,Integer userId,Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Article> articles = articleMapper.getArticlesByTitle(title,userId);
        Page<Article> p=(Page<Article>)articles;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Result addArticle(Article article) {
        try {
            articleMapper.addArticle(article);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加文章失败");
        }
    }

    @Override
    public Result updateArticle(Article article) {
        try {
            articleMapper.updateArticle(article);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新文章失败");
        }
    }

    @Override
    public Result deleteArticles(List<Integer> ids) {
        try {
            articleMapper.deleteArticles(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除文章失败");
        }
    }

    @Override
    public PageResult getArticlesByUserId(Integer userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Article> articles = articleMapper.getArticlesByUserId(userId);
        Page<Article> p=(Page<Article>)articles;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    @Override
    public List<Article> find(Integer id) {
        return articleMapper.findAllByConsultant_idArticles(id);
    }

}
